
//pauls function.  Works Great!
function validDate(date){
	//var for required element error message.
	var date_message = " <span id='error_message'>Must be a valid date.</span>";
	
	//removes error messages from previous validation.
	$(".textbox_error").removeClass();
	$("span[id='error_message']").remove();
	$("#error").hide();
	
	//pauls date validator in action with jquery.
	var validator = new pdv();
	if (date.value.length > 0) {
		if (validator.IsSQLServerDate(date.value)){
			date.value = validator.DisplayDate(date.value, 'mm/dd/yyyy');			
		}else{		
			$("#error").show();
			$("#" + date.id + " ~ img").after(date_message);
			$("#" + date.id).addClass('textbox_error');	
		}
	}	
}


var validateElements = function(){
	//sets error count to zero, if greater then zero, some data is not valid
	var error_count = 0;
	
	//var for required element error message.
	var require_message = " <span id='error_message' >Field is required.</span>";
	
	//these fields are require on form.
	var required_fields = ["AuthNum", "AuthUnits", "RemUnits"];
	
	//these fields are require on form.
	var required_fields_dates = ["PriorAuthDate", "FromDate",  "ThruDate"];
	
	//removes error messages from previous validation.
	$(".textbox_error").removeClass();
	$("span[id='error_message']").remove();
	$("#error").hide();
	
	//if fields in the required_fields variable are empty this error is triggered.
	$.map(required_fields,function(field){
		if( !$("#" + field).val() )	{
			error_count++;
			$("#error").show();
			$("#" + field).after(require_message);
			$("#" + field).addClass('textbox_error');
		}
	});
	
	//if fields in the required_fields variable are empty this error is triggered.
	$.map(required_fields_dates,function(field){
		if( !$("#" + field).val() )	{
			error_count++;
			$("#error").show();
			$("#" + field + " ~ img").after(require_message);
			$("#" + field).addClass('textbox_error');
		}
	});
		
	//if no errors in validation, well... the data is valid!
	if(error_count){return false;}else{return true;}
};


function bindElements(){	
		
	//make meal_date textbox a datepicker
	$("#PriorAuthDate, #FromDate, #ThruDate").datepicker({ 
		dateFormat: 'mm/dd/yy', changeYear: true, changeMonth: true, yearRange: '1900:2010', autoSize: true,
		prevText: 'Earlier', nextText: 'Later', showOn: 'button', 
		buttonImage: '/CFIDE/scripts/ajax/resources/cf/images/DateChooser.png', buttonImageOnly: true,
		onClose: function(dateText, inst) { $(this).focus()}
	});	
	
	//binds change event to getDataById.  This is to populate form data.
	$("#elder").bind("change", function(){
		loadElder();		
	});
	
	//binds plan drop down to load Prior Auth
	$("#prior_auths").bind('change', function(){
		window.location.href="priorauth.cfm?ElderID=" + $("#elder option:selected").val() + 
			"&PriorAuthID=" + $("#prior_auths option:selected").val();
			
		//setting session variable that holds current elder selected
		var session_elder_id = new sse();
		session_elder_id.funcSetSessionElderID( $("#elder option:selected").val() );
	});
	
	//this binds 'add' button's click event to trigger form validation before sending to database.  
	$("#save_auth").bind("click", function(){
		if ( validateElements() ){
			if( $("#prior_auths option:selected").val() == '-1' ){
				$("#myform").attr({'action': 'api.cfm?api=add_prior_auth', 'method':'POST'});
				$("#myform").submit();
			}
		}
	});
	
	//this binds 'add' button's click event to trigger form validation before sending to database.  
	$("#edit_auth").bind("click", function(){
		if ( validateElements() ){
			if( $("#prior_auths option:selected").val() != '-1' ){
				$("#myform").attr({'action': 'api.cfm?api=edit_prior_auth', 'method':'POST'});
				$("#myform").submit();
			}
		}
	});
	
	
	//this binds 'add' button's click event to trigger form validation before sending to database.  
	$("#delete_auth").bind("click", function(){
		if ( validateElements() ){
			if( $("#prior_auths option:selected").val() != '-1' ){
				$("#myform").attr({'action': 'api.cfm?api=delete_prior_auth&id=' + 
								  $("#prior_auths option:selected").val(), 'method':'POST'});
				$("#myform").submit();
			}
		}
	});

}


function loadElder(){
	//takes value of selected option in the elder dropdown and passes it to the function that fills the form.
	if($("#elder option:selected").val() != -1){			
		//setting session variable that holds current elder selected
		window.location.href="priorauth.cfm?ElderID=" + $("#elder option:selected").val();
		var session_elder_id = new sse();
		session_elder_id.funcSetSessionElderID( $("#elder option:selected").val() );
	}
	else{
		window.location.href="priorauth.cfm?ElderID=-1";
		var session_elder_id = new sse();
		session_elder_id.funcSetSessionElderID( $("#elder option:selected").val() );
		clearElements();
		$('#LastName').focus();
	}	
}



function clearElements(){
	//reset all textboxes
	$(":input:not(input[type='button']):not(input[type='hidden']):not(input[id='date_of_contact'])" + 
		":not(input[id='elder_search']):not(select[id='elder']):not(input[id='sign_date'])").val("");
	
}


//ajax POST to add to database.
function submitForm(obj, api, id, successFunc, completeFunc, errorFunc){	
	var api_url = "api.cfm?api=" + api + ( (id) ? "&id=" + id : "" );
	$.ajax({
		type:'POST', url:api_url, dataType: 'json', async: true, data: obj,
		success: function() {successFunc},
		complete: function(){completeFunc},
		error: function(){errorFunc}
	});
}

//ajax GET to retrive data from database.
var getDataById = function(api, id){
	var data = {};
	var api_url = "api.cfm?api=" + api + ( (id) ? "&id=" + id : "" );
	
	if (id != -1){
		$.ajax({
		type:'GET', url: api_url, dataType: 'json', async: false,
			success: function(json) { data = json; }
		});
	}
	
	return data;
};


$(function(){
	bindElements();
});